/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ibrae.excelparser1.mainlogics.dbwork;

import ru.ibrae.cr.domain.BaseDocument;
import ru.ibrae.cr.domain.Person;
import ru.ibrae.cr.orm.AbstractEntity;

import ru.ibrae.excelparser1.excelstorageentities.entities.CertificateInfo;
import ru.ibrae.excelparser1.excelstorageentities.entities.CommonInfo;
import ru.ibrae.excelparser1.excelstorageentities.entities.ExcelField;
import ru.ibrae.excelparser1.excelstorageentities.entities.MchsLetter;
import ru.ibrae.excelparser1.excelstorageentities.entities.OrganizationInfo;
import ru.ibrae.excelparser1.excelstorageentities.entities.PersonInfo;
import ru.ibrae.excelparser1.mainlogics.CheckResult;
import ru.ibrae.excelparser1.mainlogics.Logging.ILogger;
import ru.ibrae.excelparser1.mainlogics.Logging.implimentation.ComplexLogger;
import ru.ibrae.excelparser1.mainlogics.datapush.PushEntity;

import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author pasha
 */
public class DatabaseWorker {
    //~ Static fields/initializers ---------------------------------------------

    private static PersonInfo personInfo;
    private static MchsLetter letter;
    private static CertificateInfo newCertificateInfo;
    private static CertificateInfo oldCertificateInfo;
    private static ILogger logger      = null;
    private static Long organizationId;

    //~ Methods ----------------------------------------------------------------

    public static CheckResult<AbstractEntity> certificateCheck(ExcelField excelField,
                                                               Long orgId) {
        loadBasicData(excelField);
        organizationId = orgId;

        CheckResult<AbstractEntity> cr1 = Requester.makeCertSearch1(
            personInfo.getSecondName(),
            newCertificateInfo.getSeries(),
            newCertificateInfo.getNumber());

        if(cr1.isLoaded()) {
            excelField.getFlags().setCertificateChecked(true);
            excelField.getFlags().setPersonChecked(true);

            return new CheckResult<AbstractEntity>(true, "", cr1.getEntity());
        } else {
            CheckResult<BaseDocument> cr2 = Requester.makeCertSearch2(
                newCertificateInfo.getSeries(),
                newCertificateInfo.getNumber(),
                letter.getNumber(),
                letter.getDate(),
                orgId);

            if(cr2 != null) {
                excelField.getFlags().setCertificateChecked(true);

                return new CheckResult<AbstractEntity>(Boolean.TRUE,
                                                       "",
                                                       cr2.getEntity());
            } else {
                CheckResult<BaseDocument> cr3 = Requester.makeCertSearch3(
                    newCertificateInfo,
                    letter,
                    orgId);

                if(cr3 != null) {
                    excelField.getFlags().setCertificateChecked(true);

                    return new CheckResult<AbstractEntity>(Boolean.TRUE,
                                                           "",
                                                           cr3.getEntity());
                }
            }
        }

        return new CheckResult<AbstractEntity>(Boolean.FALSE,
                                               "",
                                               ( AbstractEntity[] )null);
    }

    public static CheckResult<Person> personCheck(ExcelField excelField) {
        loadBasicData(excelField);

        final CheckResult<Person> cr1 = Requester.makePersonSearch2(excelField);

        if(cr1 != null) {
            return cr1;
        } else {
            CheckResult<Person> cr2 = Requester.makePersonSearch3(excelField);

            if(cr2 != null) {
                return cr2;
            }
        }

        return new CheckResult<Person>(Boolean.FALSE, "", ( Person[] )null);
    }

    public static Long organiztionCheck(CommonInfo info) {
        return Requester.makeOrgSearch1(info);
    }

    private static void loadBasicData(ExcelField excelField) {
        letter = excelField.getMchsLetter();
        personInfo = letter.getPerson();
        newCertificateInfo = personInfo.getCurrentCertificate();
        oldCertificateInfo = personInfo.getPreviousCertificate();
    }

    static synchronized ILogger getLogger() {
        if(logger == null) {
            logger = new ComplexLogger();
        }

        return logger;
    }

    public static PushEntity push(PushEntity pushEntity, MchsLetter letter,
                                  OrganizationInfo info) {
        if(pushEntity.getPerson() != null) {
            try {
                Long personId = Requester.pushPersonInfo(pushEntity,
                                                         info.getOrgId(),
                                                         letter);
                pushEntity.setPersonId(personId);
                pushEntity.setLoadedSuccessfully(true);
            } catch(Exception e) {
                pushEntity.setLoadedSuccessfully(false);
            }
        } else {
            try {
                Long certificateId = Requester.pushCertificateInfo(pushEntity,
                                                                   letter,
                                                                   info);
                pushEntity.setCertificateId(certificateId);
                pushEntity.setLoadedSuccessfully(true);
            } catch(Exception ex) {
                Logger.getLogger(DatabaseWorker.class.getName()).log(Level.SEVERE,
                                                                     null,
                                                                     ex);
                pushEntity.setLoadedSuccessfully(false);
            }
        }

        return pushEntity;
    }

    public static void pushOrganization(OrganizationInfo oi) {
        Requester.pushOrganizationInfo(oi);
    }
}
